<?php
declare (strict_types = 1);

namespace app\admin\controller\projects;

use app\admin\controller\AuthController;
use app\admin\model\project\ProTypes;
use app\admin\model\system\SystemMenus;
use app\admin\model\system\SystemRole;
use bbweb\services\UtilService;
use think\Request;

class Types extends AuthController
{
    /**
     * 显示列表
     *
     * @param  \think\Request  $req
     * @return \think\Response
     */
    public function index(Request $req)
    {
        $groups = SystemRole::getSelect(0,0);
        $allAuth = in_array('64',array_keys($this->auth));

        if ($req->isAjax()) {
            $data = [];
            $page = $req->param('page/d', 1);
            $limit = $req->param('limit/d', 15);
            $query = ProTypes::order('id DESC');
            $groupid = $req->param('groupid');
            if (!is_null($groupid) && strlen($groupid)) {
                $query->where('group', $groupid);
            }
            if(!$allAuth) {
                $adminInfo = $this->adminInfo;
                $roleid = $adminInfo['roleid']??0;
                $groupid = SystemRole::where('id', $roleid)->value('pid');
                $query->where('group', $groupid);
            }
            $data['count'] = $query->count('id');
            $data['data'] = $query->page($page,$limit)->select()->toArray();

            $data['code'] = 0;
            $data['msg'] = '';
            foreach ($data['data'] as &$val) {
                $val['groups'] = $groups[$val['group']] ?? '--/--';
            }
            return json($data);
        }
        $sonAuth = SystemMenus::getSonAuth($req->controller(), $req->action(),array_keys($this->auth));
        // 显示页面
        $this->assign('sonAuth', $sonAuth);
        $this->assign('groups', $groups);
        $this->assign('allAuth', $allAuth);
        return $this->fetch();
    }

    /**
     * 创建资源.
     *
     * @param  \think\Request  $req
     * @return \think\Response
     */
    public function add(Request $req)
    {
        $allAuth = in_array('64',array_keys($this->auth));
        if($req->isPost()) {
            $data = UtilService::postMore([
                'name',
                'description',
            ]);
            if (!$data['name']) $this->error('请输入项目名称');
            $adminInfo = $this->adminInfo;
            $roleid = $adminInfo['roleid']??0;
            if($allAuth) {
                $groupid = $req->param('group/d',0);
            } else {
                $groupid = SystemRole::where('id', $roleid)->value('pid');
            }
            $data['group'] = $groupid;
            $data['update_at'] = time();

            if(ProTypes::create($data)) {
                $this->success();
            } else {
                $this->error();
            }
        }
        $groups = SystemRole::getSelect(0,0);
        $this->assign('groups', $groups);
        $this->assign('allAuth', $allAuth);
        return $this->fetch();
    }

    /**
     * 更新资源
     *
     * @param  \think\Request  $req
     * @return \think\Response
     */
    public function edit(Request $req)
    {
        $id = intval($req->param('id/d'));
        if(!$id) $this->error('参数错误', '', -1);
        $type = ProTypes::find($id);
        if(!$type) $this->error('参数错误', '', -1);
        $allAuth = in_array('64',array_keys($this->auth));
        if($req->isPost()) {
            $data = UtilService::postMore([
                'name',
                'description',
            ]);
            if (!$data['name']) $this->error('请输入项目名称');
            $adminInfo = $this->adminInfo;
            $roleid = $adminInfo['roleid']??0;
            if($allAuth) {
                $groupid = $req->param('group/d',0);
            } else {
                $groupid = SystemRole::where('id', $roleid)->value('pid');
            }
            $data['group'] = $groupid;
            $data['update_at'] = time();

            if(ProTypes::update($data, ['id'=>$id])) {
                $this->success();
            } else {
                $this->error();
            }
        }
        $this->assign('type', $type);
        $groups = SystemRole::getSelect(0,0);
        $this->assign('groups', $groups);
        $this->assign('allAuth', $allAuth);
        return $this->fetch();
    }
}
